/*
 * All rights Reserved, Designed By cloudland Copyright(C) 2010-2014
 *
 * fileName:  FieldDescImpl.java
 * author:  lei
 * Company:  Cloudland Studio
 * Createdate:  2013-9-19 下午10:04:56
 *   
 * Modification  History:
 *     Date           Author        Version        
 * -------------------------------------------
 * 2013-9-19          Lei            1.0        
 *
 * Why & What is modified:
 * 1.0: 文件创建
 */
package org.cloudland.search.config.support;

import java.util.regex.Pattern;

import org.cloudland.search.config.FieldTypeEnum;
import org.cloudland.search.config.IFieldDesc;

 /**
 * <pre> 
 * 获取配置文件中索引域的描述，包括索引字段名称，
 * 映射的数据源中key值，及索引字段是否参加索引，分词等属性，
 * 实现了IFieldDesc接口
 * </pre>
 * @ClassName  FieldDescImpl
 * @author  lei
 * @version  %I%, %G%
 * @see  
 * @since   JDK1.6
 *
 */
public class FieldDescImpl implements IFieldDesc {

private FieldDescEntity fieldDescEntity;
    
    public FieldDescImpl ()
    {
        
    }
    
    /**
     * 
     *@description: 设置索引项信息对象
     *@param fieldDescEntity 索引项信息对象
     */
    public void setFieldDescEntity(FieldDescEntity fieldDescEntity)
    {
        this.fieldDescEntity = fieldDescEntity;
    }
    
    /**
     * 
     *@description: 获取索引域名
     *@return 索引域名称
     */
    public String getFieldName()
    {
        return fieldDescEntity.getFieldName();
    }
    
    /**
     * 
     *@description: 获取索引项对应的数据源key名称
     *@return 索引项对应的数据源key名称
     */
    public String getFieldMapping()
    {
        return fieldDescEntity.getFieldMapping();
    }
    
    /**
     * 
     *@description: 索引项是否参加索引
     *@return 参加索引返回true，否则返回false
     */
    public boolean isIndex()
    {
        boolean result = true;
        
        if (fieldDescEntity.getFieldIndex()!=null 
                && "false".equals(fieldDescEntity.getFieldIndex().toLowerCase()))
        {
            result = false;
        }
        
        return result;
    }
    
    /**
     * 
     *@description: 索引项是否参加分词
     *@return 参加分词返回true，否则返回false
     */
    public boolean isAnalyzer()
    {
        boolean result = true;
        
        if (fieldDescEntity.getFieldAnalyzer()!=null 
                && "false".equals(fieldDescEntity.getFieldAnalyzer().toLowerCase()))
        {
            result = false;
        }
        
        return result;
    }
    
    /**
     * 
     *@description: 索引项是否存储
     *@return 存储返回true，否则返回false
     */
    public boolean isStore()
    {
        boolean result = true;
        
        if (fieldDescEntity.getFieldStore()!=null 
                && "false".equals(fieldDescEntity.getFieldStore().toLowerCase()))
        {
            result = false;
        }
        
        return result;
    }
    
    /**
     * 
     *@description: 索引项是否参与评分
     *@return 参与评分返回true，否则返回false
     */
    public boolean isGrade()
    {
        boolean result = true;
        
        if (fieldDescEntity.getFieldGrade() != null 
                && "false".equals(fieldDescEntity.getFieldGrade().toLowerCase()))
        {
            result = false;
        }
        
        return result;
    }
    
    /**
     * 
     *@description: 是否为索引项的唯一标识
     *@return 是唯一标识为true，否则为false
     */
    public boolean isPrimary()
    {
        boolean result = false;
        
        if (fieldDescEntity.getPrimaryKey() != null 
                && "true".equals(fieldDescEntity.getPrimaryKey().toLowerCase()))
        {
            result = true;
        }
        
        return result;
    }
    
    /**
     * 
     *@description: 获取索引项的权重
     *@return 索引项的权重
     */
    public float getWeight()
    {
        float  result = 0.0f;
        
        if (fieldDescEntity.getFieldWeight()!=null 
                && Pattern.matches("\\d+(.\\d+){0,1}", fieldDescEntity.getFieldWeight()))
        {
            result = Float.valueOf(fieldDescEntity.getFieldWeight()).floatValue();
        }
        
        return result;
    }
    
    
    /**
     * 
     *@description: 获取索引项的数据类型
     *@return 索引项的数据类型
     */
    public FieldTypeEnum getFieldType()
    {
        FieldTypeEnum result = FieldTypeEnum.TEXT;
        if (fieldDescEntity.getFieldType() != null)
        {
            if ("text".equals(fieldDescEntity.getFieldType().toLowerCase()))
            {
                result = FieldTypeEnum.TEXT;
            }
            else if ("decimal".equals(fieldDescEntity.getFieldType().toLowerCase()))
            {
                result = FieldTypeEnum.DECIMAL;
            }
            else if ("time".equals(fieldDescEntity.getFieldType().toLowerCase()))
            {
                result = FieldTypeEnum.TIME;
            }
        }
            
        return result;
    }

}
